import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:happy_study/core/model/student_model.dart';
import 'package:happy_study/ui/trainning/training_logic.dart';
import 'package:happy_study/ui/trainning/training_state.dart';
import 'package:happy_study/ui/trainning/training_view.dart';

import 'main_state.dart';

class MainLogic extends GetxController {
  final MainState state = MainState();

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void fetchStudents() {
    state.studentDao.queryAll().then((value) {
      state.studentGroups.clear();
      for (int i = 0; i < value.length; i += state.groupNum) {
        List<StudentModel> group = [];
        for (int j = i; j < i + state.groupNum; j++) {
          group.add(value[j]);
        }
        state.studentGroups.add(group);
      }
      update();
    });
  }

  ///帮助学生
  void trainStudent(student) {
    student.studentExperience += 10;
    if (student.studentExperience > 100) {
      student.studentExperience -= 100;
      student.studentLevel += 1;
    }
    state.studentDao.updateStudent(student);
    update();
  }

  void showStudentDetailDialog(StudentModel student) {
    SmartDialog.show(
      builder: (ctx) {
        return Stack(
          clipBehavior: Clip.none,
          children: [
            Container(
              color: Colors.red,
              height: .8.sh,
              width: .7.sw,
            ),
            Positioned(
              left: 10.r,
              top: 10.r,
              bottom: 10.r,
              child: Container(
                width: 80.w,
                decoration: BoxDecoration(color: Colors.green),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(student.studentName),
                    Text("${student.studentLevel}"),
                    Text("${student.studentExperience} / 100"),
                  ],
                ),
              ),
            ),
            Positioned(
              left: 95.w,
              bottom: 10.r,
              child: GestureDetector(
                onTap: () {
                  TrainingState trainingState = Get.find<TrainingLogic>().state;
                  trainingState.student = student;
                  Get.toNamed(Trainingview.routeName);
                  SmartDialog.dismiss();
                },
                child: Container(
                  color: Colors.blue,
                  width: 40.w,
                  height: 40.w,
                  child: Text("训练"),
                ),
              ),
            ),
            Positioned(
              left: 90.w,
              top: 10.r,
              child: Container(
                color: Colors.amber,
                width: 80.w,
                height: 60.w,
                child: Text("学习记录"),
              ),
            ),
            Positioned(
              top: -10.w,
              right: -10.w,
              child: Container(
                color: Colors.cyan,
                width: 80.w,
                height: 160.w,
                child: Text("人物"),
              ),
            )
          ],
        );
      },
    );
  }
}
